package com.hbsx.why.ui;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hbsx.why.R;
import com.hbsx.why.base.BaseActivity;
import com.hbsx.why.utils.LogUtils;
import com.hbsx.why.utils.PermissionUtils;
import com.hbsx.why.utils.ToastUtils;
import com.hbsx.why.utils.updateUtil.UpdateStatus;
import com.hbsx.why.utils.updateUtil.UpdateVersionUtil;
import com.hbsx.why.utils.updateUtil.VersionInfo;
import com.hbsx.why.views.BackButton;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by liub on 2017/7/26 13:38.
 */

public class ApkDownloadActivity extends BaseActivity {
    @BindView(R.id.button1)
    Button button1;
    @BindView(R.id.btn_back)
    BackButton btnBack;
    @BindView(R.id.tv_topTitle)
    TextView tvTopTitle;
    private Context context;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_apk_update;
    }

    @Override
    protected void initView() {
        context = this;
        tvTopTitle.setText("apk更新");
    }

    @OnClick({R.id.button, R.id.button1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button:

                break;
            case R.id.button1:
                if (!requiPermission()) {
                    return;
                }
                if (!requiWriterPermission()) {
                    return;
                }
                apkDownload();
                break;
        }
    }
    public void apkDownload() {
        UpdateVersionUtil.localCheckedVersion(context, new UpdateVersionUtil.UpdateListener() {
            @Override
            public void onUpdateReturned(int updateStatus, VersionInfo versionInfo) {
                //判断回调过来的版本检测状态
                switch (updateStatus) {
                    case UpdateStatus.YES:
                        //弹出更新提示
                        UpdateVersionUtil.showDialog(context, versionInfo);
                        break;
                    case UpdateStatus.NO:
                        //没有新版本
                        ToastUtils.toastShort("已经是最新版本了!");
                        break;
                    case UpdateStatus.NOWIFI:
                        //当前是非wifi网络
                        ToastUtils.toastShort("只有在wifi下更新！");
                        break;
                    case UpdateStatus.ERROR:
                        //检测失败
                        ToastUtils.toastShort("检测失败，请稍后重试！");
                        break;
                    case UpdateStatus.TIMEOUT:
                        //链接超时
                        ToastUtils.toastShort("链接超时，请检查网络设置!");
                        break;
                }
            }
        });
    }

    public boolean requiPermission() {
        final boolean[] myStatus = {false};
        PermissionUtils.requestPermission(ApkDownloadActivity.this, PermissionUtils.CODE_READ_EXTERNAL_STORAGE, new PermissionUtils.PermissionGrant() {
            @Override
            public void onPermissionGranted(int requestCode) {
                LogUtils.logi("---------requestCode=" + requestCode);
                if (requestCode == 7) {
                    myStatus[0] = true;
                }
            }
        });
        return myStatus[0];
    }

    public boolean requiWriterPermission() {
        final boolean[] myStatus = {false};
        PermissionUtils.requestPermission(ApkDownloadActivity.this, PermissionUtils.CODE_WRITE_EXTERNAL_STORAGE, new PermissionUtils.PermissionGrant() {
            @Override
            public void onPermissionGranted(int requestCode) {
                LogUtils.logi("---------requestCode=" + requestCode);
                if (requestCode == 8) {
                    myStatus[0] = true;
                }
            }
        });
        return myStatus[0];
    }

    public boolean requimulPermission() {
        final boolean[] myStatus = {false};
        PermissionUtils.requestMultiPermissions(ApkDownloadActivity.this, new PermissionUtils.PermissionGrant() {
            @Override
            public void onPermissionGranted(int requestCode) {
                LogUtils.logi("---------requestCode=" + requestCode);
            }
        });
        return myStatus[0];
    }


}
